package com.sino.business.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sino.bean.Xk;
import com.sino.business.*;
import com.sino.dao.IPatentManageStatDAO;
import com.sino.model.QxkwModel;
import com.sino.model.SbrModel;
import com.sino.model.XkModel;
import com.sino.util.SysParameter;

public class PatentManageStatServiceImp implements IPatentManageStatService {

	public List getPatentStatBySbrName(String hpl) {
		List sbrStatList = new ArrayList();
		String queryHql = "select count(*),basicinfo.sbr,basicinfo.zlid from Basicinfo as basicinfo where basicinfo.year="+SysParameter.CURRENT_YEAR+" ";
		List sbrStatlList = new ArrayList();
		if (hpl.length() > 0) {
			queryHql = queryHql + " and " + hpl;
		}
		queryHql = queryHql + " group by basicinfo.sbr";
		List qxkwList = this.getPatentManageStatDAO().findPatentStatBySbrName(
				queryHql);
		Iterator it = qxkwList.iterator();
		int max=this.getMaxBar(it);//获得专利最大的数
		Iterator it1 = qxkwList.iterator();
		while (it1.hasNext()) {
			SbrModel qm = new SbrModel();
			Object[] qot = (Object[]) it1.next();
			qm.setPatentCount(((Integer) qot[0]));
			qm.setPercentage(this.getPercentage(qm.getPatentCount(), this
					.getPatentStatAllCount()));
			qm.setBarWidth(String.valueOf(qm.getPatentCount()*(520/max)));
			qm.setSbr((String) qot[1]);
			qm.setId((String) qot[2]);
			sbrStatList.add(qm);
		}
		return sbrStatList;
	}
	IPatentManageStatDAO patentManageStatDAO;
	public List getPatentStatByQxkw() {
		List qxkwStatList = new ArrayList();
		List qxkwList = this.getPatentManageStatDAO().findPatentStatByQxkw();
		Iterator it = qxkwList.iterator();
		Iterator it1 = qxkwList.iterator();
		int max=this.getMaxBar(it);//获得专利最大的数
		
		while (it1.hasNext()) {
			QxkwModel qm = new QxkwModel();
			Object[] qot = (Object[]) it1.next();
			qm.setPatentCount((Integer) qot[0]);
			qm.setPercentage(this.getPercentage(qm.getPatentCount(), this
					.getPatentStatAllCount()));
			qm.setBarWidth(String.valueOf(qm.getPatentCount()*(556/max)));
		
			qm.setName((String) qot[1]);
			qm.setTypeid((String) qot[2]);
			qxkwStatList.add(qm);
		}
		return qxkwStatList;
	}
	public List getPatentStatByQxkw(String qxkw) {
		// this.getPatentManageStatDAO().findPatentStatByQxkw(qxkw);
		return this.getPatentManageStatDAO().findPatentStatByQxkw(qxkw);
	}
	public List getPatentStatBySbr() {
		List sbrStatList = new ArrayList();
		List qxkwList = this.getPatentManageStatDAO().findPatentStatBySbr();
		Iterator it1 = qxkwList.iterator();
		int max=this.getMaxBar(it1);//获得专利最大的数
		
		Iterator it = qxkwList.iterator();
		while (it.hasNext()) {
			SbrModel qm = new SbrModel();
			Object[] qot = (Object[]) it.next();
			qm.setPatentCount(((Integer) qot[0]));
			qm.setPercentage(this.getPercentage(qm.getPatentCount(), this
					.getPatentStatAllCount()));
			qm.setBarWidth(String.valueOf(qm.getPatentCount()*(550/max)));
			qm.setSbr((String) qot[1]);
			qm.setId((String) qot[2]);
			sbrStatList.add(qm);
		}
		return sbrStatList;
	}
	public List getPatentStatBySbr(String zlid) {
		String sbr = this.getPatentManageStatDAO().findPatentStatByZlid(zlid);
		return this.patentManageStatDAO.findPatentStatBySbr(sbr);
	}
	public List getPatentStatByZy() {
		List zyStatList = new ArrayList();
		List zyList = this.getPatentManageStatDAO().findPatentStatByZy();
		Iterator it1 = zyList.iterator();
		int max=this.getMaxBar(it1);//获得专利最大的数
		
		Iterator it = zyList.iterator();
		while (it.hasNext()) {
			XkModel xm = new XkModel();
			Object[] qot = (Object[]) it.next();
			xm.setPatentCount((Integer) qot[0]);
			xm.setPercentage(this.getPercentage(xm.getPatentCount(), this
					.getPatentStatAllCount()));
			xm.setBarWidth(String.valueOf(xm.getPatentCount()*(500/max)));
			xm.setName((String) qot[1]);
			xm.setId(((String) qot[2]));
			zyStatList.add(xm);
		}
		return zyStatList;
	}
	public List getPatentStatByZy(String zy) {
		return this.getPatentManageStatDAO().findPatentStatByZy(zy);
	}
	public XkModel getPatentStatByZy(XkModel xm) {
		Xk xk = this.patentManageStatDAO.findPatentStatById(xm.getId());
		xm.setId(xk.getId());
		xm.setName(xk.getName());
		return xm;
	}

	public IPatentManageStatDAO getPatentManageStatDAO() {
		return patentManageStatDAO;
	}
	public void setPatentManageStatDAO(IPatentManageStatDAO patentManageStatDAO) {
		this.patentManageStatDAO = patentManageStatDAO;
	}
	public List getPatentStatBySbrWithUnit() {
		List sbrStatList = new ArrayList();
		List qxkwList = this.getPatentManageStatDAO()
				.findPatentStatBySbrWithUnit();
		Iterator it = qxkwList.iterator();
		Iterator it1 = qxkwList.iterator();
		int max=this.getMaxBar(it1);//获得专利最大的数
		
		while (it.hasNext()) {
			SbrModel qm = new SbrModel();
			Object[] qot = (Object[]) it.next();
			qm.setPatentCount(((Integer) qot[0]));
			qm.setPercentage(this.getPercentage(qm.getPatentCount(), this
					.getPatentStatAllCount()));
			qm.setBarWidth(String.valueOf(qm.getPatentCount()*(500/max)));
			qm.setSbr((String) qot[1]);
			qm.setId((String) qot[2]);
			sbrStatList.add(qm);

		}
		return sbrStatList;
	}

	/**
	 * 申请单位
	 */
	public List getPatentStatBySbrWithUnit(String zlid) {
		String sbr = this.getPatentManageStatDAO().findPatentStatByZlid(zlid);
		return this.patentManageStatDAO.findPatentStatBySbrWithUnit(sbr);
	}

	public List getPatentStatByQxkw1(String hpl) {
		String queryHql = "select count(*),basicinfo.qxkw.name,basicinfo.qxkw.id from Basicinfo as basicinfo where basicinfo.year="+SysParameter.CURRENT_YEAR+" ";;

		List sbrStatlList = new ArrayList();
		if (hpl.length() > 0) {
			queryHql = queryHql + " and " + hpl;
		}
		queryHql = queryHql + " group by basicinfo.qxkw.id";

		List qxkwList = this.getPatentManageStatDAO().findPatentStatByQxkw1(
				queryHql);
		List qxkwStatList = new ArrayList();
		Iterator it1 = qxkwList.iterator();
		int max=this.getMaxBar(it1);//获得专利最大的数

		Iterator it = qxkwList.iterator();
		while (it.hasNext()) {
			QxkwModel qm = new QxkwModel();
			Object[] qot = (Object[]) it.next();
			qm.setPatentCount((Integer) qot[0]);
			qm.setPercentage(this.getPercentage(qm.getPatentCount(), this
					.getPatentStatAllCount()));
			qm.setBarWidth(String.valueOf(qm.getPatentCount()*(540/max)));
			qm.setName((String) qot[1]);
			qm.setTypeid((String) qot[2]);
			qxkwStatList.add(qm);

		}
		return qxkwStatList;
	}

	public int getPatentStatAllCount() {
		List patentAllCount = this.patentManageStatDAO.findPatentStatAllCount();

		return ((Integer) patentAllCount.get(0));// 返回
	}

	public String getPercentage(int count, int allCount) {//计算百分比
		Double d = 100d * count / allCount;

		String strd = Double.toString(d.doubleValue());
		String endStr;

		int len = strd.length();
		if (strd.substring(strd.indexOf("."), len).length() >= 3) {
			endStr = strd.substring(0, strd.indexOf(".") + 3);
		} else {
			endStr = strd.substring(0, strd.indexOf("."));
		}

		return endStr + "%";
	}
   public int getBar(){   //获得柱状图的长度
	return 0;   
   }
   public int getMaxBar(Iterator it){ //获得最长的柱状图值
	   int max=0;
	   while (it.hasNext()){
	   Object[] o=(Object[])it.next();
	  
	   if(max<(Integer)o[0]){
		  max=(Integer)o[0]; 
	   
	   }
    }
	   return max;   
	   
   }
   

}
